// Sleep与boost::this_thread::sleep均不够精确，精度基本无差别，延迟1ms时范围通常为0.8-3ms间

#include <stdio.h>
#include <windows.h>
#include <boost/thread.hpp>

int main() {
    LARGE_INTEGER nFreq;
    LARGE_INTEGER nBeginTime;
    LARGE_INTEGER nEndTime;
    double time1, time2;
    int i1 = 0, i2 = 0;
    double total_time1 = 0, total_time2 = 0;

    QueryPerformanceFrequency(&nFreq);
    printf("%lld\n", nFreq.QuadPart);

    int queryNum = 1000;
    int delayMilliseconds = 2;
    for(int i = 0; i< queryNum; i++) {
        QueryPerformanceCounter(&nBeginTime);
        Sleep(delayMilliseconds);
        QueryPerformanceCounter(&nEndTime);
        time1 = (double) (nEndTime.QuadPart - nBeginTime.QuadPart) / ((double) nFreq.QuadPart / 1000);
        total_time1 += time1;
        printf("%f\n", time1);
        printf("%lld - %lld = %lld\n", nEndTime.QuadPart, nBeginTime.QuadPart, nEndTime.QuadPart - nBeginTime.QuadPart);

        QueryPerformanceCounter(&nBeginTime);
        boost::this_thread::sleep(boost::posix_time::milliseconds(delayMilliseconds));
        QueryPerformanceCounter(&nEndTime);
        time2 = (double) (nEndTime.QuadPart - nBeginTime.QuadPart) / ((double) nFreq.QuadPart / 1000);
        total_time2 += time2;
        printf("%f\n", time2);
        printf("%lld - %lld = %lld\n", nEndTime.QuadPart, nBeginTime.QuadPart, nEndTime.QuadPart - nBeginTime.QuadPart);

        time1 < time2 ? i1++ : i2++;
    }

    printf("%d %d\n", i1, i2);
    printf("%f %f\n", total_time1 / queryNum, total_time2 / queryNum);
    return 0;
}
